﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.IO;

public partial class Admin_Upload : System.Web.UI.Page
{
    public string id = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        // ClientID cua textbox trong cua so cha
        // dung id de dua gia tri vao textbox do
        id = Request.QueryString["id"] ?? "";
        if (!IsPostBack)
            PopulateControls();
    }
    private void PopulateControls()
    {
        // So hinh anh tren mot trang
        int pageSize = GlobalConfiguration.ImagesPerPage;
        // Thu muc chua hinh anh
        string imageFolder = MapPath("~/ProductImages");
        DirectoryInfo dir = new DirectoryInfo(imageFolder);
        FileInfo[] allFiles = dir.GetFiles();
        // Dung LINQ: Tong so hinh trong thu muc 
        int totalFiles = allFiles.Length;
        string page = Request.QueryString["Page"] ?? "1";
        int howManyPages = (int)Math.Ceiling((double)totalFiles / (double)pageSize);
        int currentPage = int.Parse(page);
        // Su ding LINQ: chi lay nhung file hinh cua trang hien tai
        // sap xep giam dan theo ngay upload 
        int skip = (currentPage - 1) * pageSize;
        FileInfo[] files = allFiles.OrderByDescending(x => x.CreationTime)
        .Skip(skip).Take(pageSize).ToArray();
        // Dua vao DataList
        dlImage.DataSource = files;
        dlImage.DataBind();
        // Thiet lap cac thong so cho Pager
        string firstPageUrl = Link.ToAdminUpload(id);
        string pagerUrl = Link.ToAdminUpload(id, "{0}");
        pagerTop.Show(currentPage, howManyPages, firstPageUrl, pagerUrl, false);
        pagerBottom.Show(currentPage, howManyPages, firstPageUrl, pagerUrl, true);
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        btnUpload.Click += new EventHandler(btnUpload_Click);
    }
    bool CheckFileType(string fileName)
    {
        string ext = Path.GetExtension(fileName);
        switch (ext.ToLower())
        {
            case ".gif":
                return true;
            case ".png":
                return true;
            case ".jpg":
                return true;
            case ".jpeg":
                return true;
            default:
                return false;
        }
    }
    void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            // Kiem tra co file upload va kich thuoc file <= 500KB
            if (upImage.HasFile && upImage.PostedFile.ContentLength <= 500000)
            {
                // Kiem tra loai file
                if (CheckFileType(upImage.FileName))
                {
                    // Lay duong dan tuong doi o server
                    String filePath = "~/ProductImages/" + upImage.FileName;
                    // Luu vao duong dan o tren
                    // MapPath(filePath) ==> chuyen ve duong dan tuyet doi
                    upImage.SaveAs(MapPath(filePath));
                    lblStatus.Text = "Upload successful";
                    PopulateControls();
                }
            }
            else
            {
                lblStatus.Text = "Uploading image failed";
            }
        }
        catch (Exception)
        {
            lblStatus.Text = "Uploading image failed";
        }
    }
}